package jude.java.money.test.init;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.framework.test.SpringTestCase;
import jude.java.money.beans.WebSite;
import jude.java.money.service.GlobalService;
import jude.java.money.spring.Services;
import jude.java.util.FileUtils;

public class InitParasitize extends SpringTestCase {

	public void _testInitFtpSites() throws IOException {
		InitBlackLink.insertFtpSites("M:\\Users\\Jude\\Desktop\\QQRecv\\0514-0515.xls");
	}
	public void _testInitFtpSites2() throws IOException {
		InitBlackLink.insertFtpSites2("M:\\Users\\Jude\\Desktop\\QQRecv\\myftp.xls", 0);
	}
	public void _testInitFtpSites3() throws IOException {
		InitBlackLink.insertFtpSites3("M:\\Users\\Jude\\Desktop\\QQRecv\\new(1).xls");
	}
	
	public void _testInitWebSites() {
		String content = FileUtils.readFile(new File("M:\\Users\\Jude\\Desktop\\0513.txt"), "GBK").toString();
		String regexp = "<a\\s+href=\\\"([^\\\"]+)\\\"[^>]*>([^<]+)</a>";
		Pattern p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
		Matcher m = p.matcher(content);
		GlobalService service = Services.getGlobalService();
		int index = 0;
		while (m.find(index)) {
			int count = m.groupCount();
			index = m.end(count) - 1;
			if (count != 2) {
				continue;
			}
			String url = m.group(1);
			String name = m.group(2);
			WebSite site = service.get(WebSite.class, "url", url);
			if (site != null) {
				if (!name.equals(site.getName())) {
					site.setName(name);
					service.update(site);
				}
			} else {
				site = new WebSite(name, url);
				service.insert(site);
			}
		}
	}

}
